package cn.xiuxius.embedded.toolwindow.model;

import lombok.experimental.Accessors;

import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

@Accessors(chain = true)
public class FloatCommandOption extends AbstractCommandOption<Float> {

    private Function<String, Boolean> validator;

    public FloatCommandOption(String cmdFlag, float defaultValue) {
        super(cmdFlag, String.format("%.1f", defaultValue), false);
    }
    @Override
    public AbstractCommandOption<Float> options(Supplier<List<Float>> provider) {
        List<String> formatted = provider.get().stream()
                .map(f -> String.format("%.1f", f))
                .toList();
        super.setOptions(formatted);
        return this;
    }
    public FloatCommandOption validator(Function<String, Boolean> validator) {
        this.validator = validator;
        return this;
    }


    @Override
    public boolean isValid() {
        if (validator != null) {
            return validator.apply(getCmdValue());
        }
        return getOptions().contains(getCmdValue());
    }

    @Override
    public boolean isValidValue(String value) {
        if (validator != null) {
            return validator.apply(value);
        }
        return getOptions().contains(value);
    }
}
